var value = {
  //baseUrl:"https://www.mgcityphoto.com/baseweb",
	baseUrl:"http://192.168.10.214:8080/baseweb",
  bid:1,
	phone:15281060106,
	serviceName:"糖糖",//服务专员
}

/*
 *请求商户信息
 * 
 */
var _loadedBusinessInfo = {}
var requestBusinessInfo = function(){
  wx.request({
    url: value.baseUrl + '/app/aboutus/aboutus',
    data: {
      bid: value.bid
    },
    method: "GET",
    success: function (res) {
      if (res.data.code == 200) {
        _loadedBusinessInfo = res.data.data
      }
    }
  })
}

var loadBusinessInfo = function(){
  return _loadedBusinessInfo
}

var callServicePhone = function(){
  var phone = _loadedBusinessInfo.phone
  if(phone){
    wx.makePhoneCall({
      phoneNumber: phone
    })
  }
}

var updateUserPhone = function(phone){

	var storeUserInfo = wx.getStorageSync('sessionUser')
	if(!storeUserInfo){
		return
	}

	storeUserInfo.phone = phone;
	wx.setStorage({
		key:"sessionUser",
		data:storeUserInfo
	})
}

/*
 *获取储存的用户信息
 */
var getStoredUserInfo = function(){
	return wx.getStorageSync('sessionUser')
}

var setStoredUserInfo = function(userInfo){
  wx.setStorage({
    key: 'sessionUser',
    data: userInfo,
  })
}

/*
 * 请求用户登录信息（有可能用户拒绝的呢牢固）
 * userinfo
 * 
 * --废弃
 */
var getUserInfo = function(callback){

	try {
		var storeUserInfo = wx.getStorageSync('sessionUser')
		if (storeUserInfo) {
			callback(storeUserInfo)
			return
		}
	} catch (e) {
		console.log(e)
	}

	getUserInfo_login(function(code){
		if(code){
			getUserInfo_getUserInfo(function(userInfo){
				if(userInfo){
					userInfo.loginCode = code
					userInfo.bid = value.bid
					
					wx.request({
						url: value.baseUrl+'/app/user/login',
						header:{
							'content-type':'application/x-www-form-urlencoded'
						},
						data:userInfo,
						method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
						success: function(res){
							// success
							console.log(res.data)
							if(res.data.code == 200){
								callback(res.data.data)
								wx.setStorage({
									key:"sessionUser",
									data:res.data.data
								})
							}
							else{
								callback()
							}
						},
						fail: function() {
							// fail
							console.log("失败")
							callback()
						},
						complete: function() {
							// complete
						}
					})
				}else{
					callback()
				}
			})
		}else{
			callback()
		}
	})
}
// 登录 callback - code
var getUserInfo_login = function(callback){
	wx.login({success:function(res){
		callback(res.code)
	}})
}

//获取用于信息
var getUserInfo_getUserInfo = function(callback){

	wx.getUserInfo({
	  success: function(res){
			callback(res.userInfo)
	  },
	  fail: function() {
			callback()
	  },
	  complete: function() {
		// complete
	  }
	})
}

/*
 *工具函数
 */

Date.prototype.format = function(fmt) { //author: meizz   
	var o = {
		"M+" : this.getMonth() + 1, //月份   
		"d+" : this.getDate(), //日   
		"h+" : this.getHours(), //小时   
		"m+" : this.getMinutes(), //分   
		"s+" : this.getSeconds(), //秒   
		"q+" : Math.floor((this.getMonth() + 3) / 3), //季度   
		"S" : this.getMilliseconds()
	//毫秒   
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	for ( var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
					: (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

function dateFrom(date,fmt){
    if(!fmt){
        fmt = "yyyy-MM-dd hh:mm";
    }
	return new Date(Date.parse(date)).format(fmt);
}

/*
 *aler弹窗
 */

function alert(msg){
	wx.showModal({
            content: msg,
            showCancel: false,
            success: function (res) {
                if (res.confirm) {
                    console.log('用户点击确定')
                }
            }
        });
}

/**
 * 检查手机号
 */
module.exports.checkMobile = function(phone){
  if (!phone) {
    return false;
  }
  if (!phone.length) {
    return false;
  }
  if (!(/^1[34578]\d{9}$/.test(phone))) {
    return false;
  }
  return true;
}


module.exports.value = value
module.exports.dateFrom = dateFrom
module.exports.getUserInfo = getUserInfo
module.exports.alert = alert
module.exports.updateUserPhone= updateUserPhone
module.exports.getStoredUserInfo = getStoredUserInfo
module.exports.setStoredUserInfo = setStoredUserInfo
module.exports.callServicePhone = callServicePhone
module.exports.requestBusinessInfo = requestBusinessInfo
module.exports.loadBusinessInfo = loadBusinessInfo